﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Collections;
using System.Reflection;
using System.Web.UI;
using MvcContrib;

namespace JqueryMvc.UI.WebControls
{
	public class ContentPlaceHolder : System.Web.UI.WebControls.ContentPlaceHolder
	{
        static PropertyInfo templatesP = typeof(MasterPage).GetProperty("ContentTemplates",
                System.Reflection.BindingFlags.GetProperty |
                System.Reflection.BindingFlags.Instance |
                System.Reflection.BindingFlags.NonPublic);

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            IDictionary templates = templatesP.GetValue(this.Page.Master, null) as IDictionary;
            if (this.Page is ViewPage && this.Controls.Count == 0 && ((templates == null) || templates[this.ID] == null))
            {
                /*ComponentViewLocator viewLocator = new ComponentViewLocator();
                string path = viewLocator.GetViewPath(
                    ((ViewPage)this.Page).ViewContext, 
                    ((ViewPage)this.Page).ViewContext.ViewName);
                if (!String.IsNullOrEmpty(path))
                    this.Controls.Add(this.Page.LoadControl(path));*/
                ((ViewPage)Page).Html.RenderPartial("_" + ((ViewPage)this.Page).ViewContext.ViewName);
            }
            base.Render(writer);
        }
	}
}
